<?php
/*----------------------------------------------------------------------
 * 影院管理
 * Author: ming.nie
----------------------------------------------------------------------*/

namespace Admin\Controller;

use Think\Controller;
class CinemaController extends AdminController {
    
    //影院用户列表
    public function cinemaList(){
        
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        //查询条件参数
        $params = array();
        $params['page_size'] = 20;
		$params['keywords'] = I('request.keywords');
		if(I('request.status') != 0){
			$params['status'] = I('request.status');
		}
		if(I('request.hoststatus') != 0){
			$params['hoststatus'] = I('request.hoststatus');
		}

        if(I('request.CopyrightIndustryOptions') > 0){
            $params['CopyrightIndustryOptions'] = I('request.CopyrightIndustryOptions');
        }

        if(I('request.type_name') > 0){
            $params['type'] = I('request.type_name');
        }

        $params['create_time_start'] = I('request.create_time_start');
        $params['create_time_end'] = I('request.create_time_end');
        $params['not_types'] = '2,3';//后台不调用黄页影院和Kim影院
        $data_list_data = $cinemaLogic->getCinemaList($params);

        //数据加工处理
        $getCinemaTypeOptions=getCinemaTypeOptions();
        $getVideoCopyrightIndustryOptions=getVideoCopyrightIndustryOptions();
        foreach ($data_list_data['data_list'] as $key=>$rs){
            //获取影院行业
            if(!empty($rs['video_copyright_industry']) && $rs['video_copyright_industry'] > 0){
                $video_copyright_industry=explode(',',$rs['video_copyright_industry']);
                $CopyrightIndustryOptions=array();
                foreach ($video_copyright_industry as $ds){
                    $CopyrightIndustryOptions[]=$getVideoCopyrightIndustryOptions[$ds];
                }
                $data_list_data['data_list'][$key]['video_copyright_industry']=implode(',',array_unique($CopyrightIndustryOptions));
            }

            //获取影院类型
            $data_list_data['data_list'][$key]['type_name']=$getCinemaTypeOptions[$rs['type']];
        }

        //获取影院用户状态
        $cinema_status_options = getCinemaStatusOptions();
        
        //获取影院计费方式
        $recharge_method_options = $cinemaLogic->getRechargeMethodOptions();
        
		//当前时间
		$current_time = time();

        //渠道商列表
		$this->assign('cinema_type_options',$getCinemaTypeOptions);
        $this->assign('VideoCopyrightIndustryOptions',$getVideoCopyrightIndustryOptions);
        //代理选项
        $agent_options = D('Admin/agent')->getAgentOption();
        $agent_options = array(''=>'平台直属影院')+$agent_options;
		$page_title = '影院列表';
        $this->assign(get_defined_vars());
        $this->display('cinemaList');
    }
	
	//影院列表  Author@wangzheng
	public function acountList(){
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        //查询条件参数
        $params = array();
        $params['page_size'] = 20;
		$params['keywords'] = I('request.keywords');
		if(I('request.status') != 0){
			$params['status'] = I('request.status');	
		}
		if(I('request.hoststatus') != 0){
			$params['hoststatus'] = I('request.hoststatus');
		}
		$params['create_time_start'] = I('request.create_time_start');
		$params['create_time_end'] = I('request.create_time_end');
        $params['not_types'] = '2,3';//后台只调取1905影院
        $data_list_data = $cinemaLogic->getCinemaList($params);
        
        //获取影院用户状态
        $cinema_status_options = getCinemaStatusOptions();
        
        //获取影院计费方式
        $recharge_method_options = $cinemaLogic->getRechargeMethodOptions();
        
        //代理选项
        $agent_options = D('Admin/agent')->getAgentOption();
        $agent_options = array(''=>'平台直属影院')+$agent_options;
		
		//统计
		$CinemaCount = M('cinema_host')->count();
		$PingCount   = M('cinema_host')->where('agent_id = 0')->count();
		$AgentCount   = M('cinema_host')->where('agent_id != 0')->count();
		$page_title = '账户列表';

        $this->assign(get_defined_vars());
        $this->display('acountList');
	}

	//影院会员列表  Author@wangzheng
	public function memberList(){

		/*查询条件*/ 
		//有效期查询
		if($_GET['create_time_start'] && $_GET['create_time_end']){
			$map['mt_member.ctime'] = array(array('EGT',strtotime($_GET['create_time_start'])  ),array('ELT',strtotime($_GET['create_time_end'])+86400)) ;
		}elseif($_GET['create_time_start']){
			$map['mt_member.ctime'] = array('EGT',strtotime($_GET['create_time_start']));
		}elseif($_GET['create_time_end']){
			$map['mt_member.ctime'] = array('ELT',strtotime($_GET['create_time_end']));
		}
		
		//根据影院名称查询
		$keywords = $_GET['keywords'];
		if($keywords){
			$map['mt_cinema_host.cinema_name'] = array('like',"%$keywords%");
		}
		if($keywords == '0'){
			$map['mt_cinema_host.cinema_name'] = array('like',"%$keywords%");
		}
		
		// 查询地区影院
		if($_GET['mutil_level_data_1_id_1']){
			$temp['area_1'] = array('EQ',$_GET['mutil_level_data_1_id_1']);
		}
		if($_GET['mutil_level_data_1_id_2']){
			$temp['area_2'] = array('EQ',$_GET['mutil_level_data_1_id_2']);
		}
		if($_GET['mutil_level_data_1_id_3']){
			$temp['area_3'] = array('EQ',$_GET['mutil_level_data_1_id_3']);
		}
		if($_GET['mutil_level_data_1_id_1'] || $_GET['mutil_level_data_1_id_2']  || $_GET['mutil_level_data_1_id_3'] || $keywords ){
			$temp['mt_cinema_host.cinema_name'] = array('like',"%$keywords%");
			$cinameList = M('cinema_host')->where($temp)->select();
			$cinemaCount = M('cinema_host')->where($temp)->count();
			$arr = array();
			foreach ($cinameList as $v){
				array_push($arr,$v['host_name']);
			}
			//如果影院地址不为空
			if($arr){
				$map['mt_member.host_name'] = array('in',$arr);
			}else{
				$map['mt_member.host_name'] = 1 ; //防止有地区，没有影院信息会查询所有影院
			}			
		}else{
			$cinemaCount = M('cinema_host')->count();
		}
		$count  = M('member')
				->join('LEFT JOIN mt_cinema_host ON mt_member.host_name = mt_cinema_host.host_name')
				->where($map)
				->count();// 查询满足要求的总记录数
		$Page   = new \Think\Page2($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(10)
		$show   = $Page->show();// 分页显示输出
		$list   = M('member')
				->field('mt_member.id, mt_member.host_name, mt_member.name, mt_member.phone_number, mt_member.out_total_amount')
				->join('LEFT JOIN mt_cinema_host ON mt_member.host_name = mt_cinema_host.host_name')
				->where($map)
				->order('mt_member.id desc')
				->limit($Page->firstRow.','.$Page->listRows)
				->select();
//echo  M('member')->getLastSql();
		$page_title = '影院会员列表';
		$this->assign('page_title',$page_title);  
		$this->assign('list',$list);    //数据列表
		$this->assign('page',$show);    //分页	
		$this->assign('count',$count);    //会员总数
		$this->assign('cinemaCount',$cinemaCount);    //影院数量			
		$this->display();
	}



	//影院会员详情  Author@wangzheng
	public function memberDetail(){
		$map['id'] = I('request.id');
		$member_info = M('member')->where($map)->find();
		$temp['host_name'] = $member_info['host_name'];
		$cinema_info = M('cinema_host')->where($temp)->find();
		$page_title = '详情';
		$this->assign('page_title',$page_title);
		$this->assign('member_info',$member_info);
		$this->assign('cinema_info',$cinema_info);
		$this->display();
	}		
    
    //添加影院用户
    public function add()
    {
        $userLogic = new \Common\Logic\UserLogic();
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        $id = I('request.id');
		
		$default_try_day_max = date('Y-m-d', strtotime('+7 days', time()));
        
        if(IS_POST) {
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            
            $params = array();
            $params['host_name'] = I('post.host_name' ,'', 'strip_tags');
			$params['host_ip'] = I('post.host_ip' ,'', 'strip_tags');
            $params['user_name'] = I('post.user_name' ,'', 'strip_tags');
            $params['area_1'] = I('post.mutil_level_data_1_id_1' , 0);
            $params['area_2'] = I('post.mutil_level_data_1_id_2' , 0);
            $params['area_3'] = I('post.mutil_level_data_1_id_3' , 0);
            $params['address'] = I('post.address' ,'', 'strip_tags');
            $params['contacter'] = I('post.contacter' ,'', 'strip_tags');
            $params['phone'] = I('post.phone' ,'', 'strip_tags');
            $params['recharge_method_id'] = I('post.recharge_method_id', 0);
            $params['agent_id'] = I('post.agent_id', 0);
			$params['agent_level_1'] = I('post.agent_level_1', 0);
			$params['agent_level_2'] = I('post.agent_level_2', 0);
			$params['type'] = I('post.type', 1);
			$params['is_try'] = I('post.is_try', 0);
			$params['try_day_max'] = I('post.try_day_max', 0);
			$params['video_limit'] = I('post.video_limit', 0);

			if($params['try_day_max']) {
				$params['try_day_max'] = strtotime($params['try_day_max'].' 23:59:59');
			}
			if(I('post.accredit_day_end')!=''){ //授权结束日期
                $params['accredit_day_end']=strtotime(I('post.accredit_day_end').' 23:59:59');
            }

			if(1 != $params['is_try']) {
				$params['try_day_max'] = 0;
			}
			
			$params['pay_position'] = I('post.pay_position', 0);
            $params['hall_price'] = I('post.hall_price', 0);
            $params['buy_hall_count'] = I('post.buy_hall_count', 0);
            $params['period_count'] = I('post.period_count', 0);
            $params['deduct_period'] = I('post.deduct_period', 0);
            $params['remark'] = I('post.remark', 0);
            $params['vpn_cinema_name'] = I('post.vpn_cinema_name','','trim,strip_tags');

			//2016计费方式
			$params['billing_video_play_type'] = I('post.billing_video_play_type');
			
			//片库类型
			$params['video_data_type'] = I('post.video_data_type');
			$params['video_net'] = I('post.video_net');
			
			//影院系统平台
			$params['system_platform'] = I('post.system_platform', 1);
			
			//是否支持本地视频
			$params['is_support_local_video'] = I('post.is_support_local_video', 2);
			
			//视频版权区域
			$params['video_copyright_area'] = I('post.video_copyright_area');
			//$params['video_copyright_area'] && $params['video_copyright_area'] = implode(',', $params['video_copyright_area']);
			//!$params['video_copyright_area'] && $params['video_copyright_area'] = '';
			
			//视频版权行业
			$params['video_copyright_industry'] = I('post.video_copyright_industry');
			//$params['video_copyright_industry'] && $params['video_copyright_industry'] = implode(',', $params['video_copyright_industry']);
			//!$params['video_copyright_industry'] && $params['video_copyright_industry'] = '';
			
			//影院签订的版权商
			$params['copyright_user_ids'] = I('post.copyright_user_ids');
			$params['copyright_user_ids'] && $params['copyright_user_ids'] = implode(',', $params['copyright_user_ids']);
			!$params['copyright_user_ids'] && $params['copyright_user_ids'] = '';
			
			
			$params['bank_card_account'] = I('post.bank_card_account' ,'', 'strip_tags');
			$params['bank_card_type'] = I('post.bank_card_type');
			$params['bank_card_bank'] = I('post.bank_card_bank');
			$params['bank_card_number'] = I('post.bank_card_number' ,'', 'strip_tags');
			$params['bank_card_address'] = I('post.bank_card_address' ,'', 'strip_tags');
			
			$params['auth_pic'] = I('post.auth_pic' ,'', 'strip_tags');
			$params['auth_code_1'] = I('post.auth_code_1' ,'', 'strip_tags');
			$params['auth_code_2'] = I('post.auth_code_2' ,'', 'strip_tags');
			
            $params['id'] = $id;
            $params['user_id'] = $user_info['user_id'];

            $result = $cinemaLogic->add($params);

            if(1 == $result['status_code']) {
				
				//数据处理成功，如果是影院类型为小帅，或者携旅，更新数据到小帅表
				if(4 == $params['type']) {
					$xs_hotel_params = array();
					$xs_hotel_params['host_name'] = $params['host_name'];
					$xs_hotel_params['qudao_type'] = 0;
					$cinemaLogic->updateXsHotel($xs_hotel_params);
				}elseif(8 == $params['type']) {
					$xs_hotel_params = array();
					$xs_hotel_params['host_name'] = $params['host_name'];
					$xs_hotel_params['qudao_type'] = 1;
					$cinemaLogic->updateXsHotel($xs_hotel_params);
				}elseif(9 == $params['type']) {
					$xs_hotel_params = array();
					$xs_hotel_params['host_name'] = $params['host_name'];
					$xs_hotel_params['qudao_type'] = 2;
					$cinemaLogic->updateXsHotel($xs_hotel_params);
				}elseif(10 == $params['type']) {
					$xs_hotel_params = array();
					$xs_hotel_params['host_name'] = $params['host_name'];
					$xs_hotel_params['qudao_type'] = 3;
					$cinemaLogic->updateXsHotel($xs_hotel_params);
				}elseif(11 == $params['type']) {
					$xs_hotel_params = array();
					$xs_hotel_params['host_name'] = $params['host_name'];
					$xs_hotel_params['qudao_type'] = 4;
					$cinemaLogic->updateXsHotel($xs_hotel_params);
				}elseif(3 == $params['type']){
                    //D('Common/KmiCron','Logic')->createKmiCinema($result['id']);
                }

                $params['type']!=2 AND D('Common/KmiCron','Logic')->createKmiCinema($result['id'],$params['type']); //创建播放日志表
                $this->success($result['status_message'], U('Cinema/cinemaList'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }

        //获取用户组选项
        $user_group_options = $userLogic->getUserGroupOptions();
        
        //获取影院计费方式
        $recharge_method_params = array();
        $recharge_method_params['status'] = 1;
		$recharge_method_params['empty'] = 1;
		$recharge_method_params['recharge_method_ids'] = '13,14';
        $recharge_method_options = $cinemaLogic->getRechargeMethodOptions($recharge_method_params);
        
        //代理选项
        $agent_options = D('Admin/agent')->getAgentOption();
        $agent_options = array(''=>'平台直属影院')+$agent_options;
        
        //扣费周期
        $deduct_period_options = getDeductPeriodOptions();
        
		//影院类型
        $cinema_type_options = getCinemaTypeOptions();
		
		//扣费先后
        $pay_position_options = getPayPositionOptions();
		
		//是否选项
		$yes_no_options = getYesOrNo();
		
		//银行选项
        $bank_options = getBankOptions(1);
		
		//银行卡类型选项
        $bank_card_type_options = getBankCardTypeOptions(1);
		
		//片源类型
        $video_data_type_options = getVideoDataTypeOptions(1);
		
		//片源网络
        $video_net_options = getVideoNetOptions();
		
		//2016计费方式
		$billing_video_play_type = C('CINEMA_BILLING_VIDEO_PLAY_TYPE');
		$billing_video_play_type = array(0=>'请选择') + $billing_video_play_type;
		
		//获取影院系统平台
        $system_platform_options = getSystemPlatformOptions();
		
		//影院影片版权区域
        $video_copyright_area_options = getVideoCopyrightAreaOptions(1);
		$checkbox_params = array();
        $checkbox_params['options'] = $video_copyright_area_options;
        $checkbox_params['name'] = 'video_copyright_area';
        $checkbox_params['separator'] = '&nbsp;&nbsp;&nbsp;&nbsp;';
        $checkbox_params['class'] = 'video_copyright_area';
        $video_copyright_area_options_form = formCheckbox($checkbox_params);
		$video_copyright_area_options = array(''=>'请选择') + $video_copyright_area_options;
		
		//影院影片版权行业
        $video_copyright_industry_options = getVideoCopyrightIndustryOptions(1);
		$checkbox_params = array();
        $checkbox_params['options'] = $video_copyright_industry_options;
        $checkbox_params['name'] = 'video_copyright_industry';
        $checkbox_params['separator'] = '&nbsp;&nbsp;&nbsp;&nbsp;';
        $checkbox_params['class'] = 'video_copyright_industry';
        $video_copyright_industry_options_form = formCheckbox($checkbox_params);
		$video_copyright_industry_options = array(''=>'请选择') + $video_copyright_industry_options;
		
		//版权商选项
		$configModel = M('Config');
		$video_film_source_options = $configModel->where('k="video_film_source"')->find();
		$video_film_source_options = json_decode($video_film_source_options['v'], true);
		$checkbox_params = array();
        $checkbox_params['options'] = $video_film_source_options;
        $checkbox_params['name'] = 'copyright_user_ids';
        $checkbox_params['separator'] = '&nbsp;&nbsp;&nbsp;&nbsp;';
        $checkbox_params['class'] = 'copyright_user_ids';
        $checkbox_params['checked'] = $model['copyright_user_ids_arr'];
        $video_film_source_options_form = formCheckbox($checkbox_params);
		
        $is_add = 1;
        $page_title = '添加用户';
        $this->assign(get_defined_vars());
        $this->display('cinema');
    }
    
    //编辑影院用户
    public function edit(){

        $userLogic = new \Common\Logic\UserLogic();
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        $id = I('request.id');
        
		//查询当前影院用户
        $model = $cinemaLogic->find($id);

		//试用日期
		if($model['try_day_max']) {
			$model['try_day_max'] = date('Y-m-d', $model['try_day_max']);
		} else {
			$model['try_day_max'] = date('Y-m-d', strtotime('+7 days', time()));
		}

        if(!empty($model['accredit_day_end'])){  //授权结束日期
            $model['accredit_day_end'] = date('Y-m-d', $model['accredit_day_end']);
        }

        if(IS_POST) {
			
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            
            $params = array();
			$params['host_ip'] = I('post.host_ip' ,'', 'strip_tags');
            $params['user_name'] = I('post.user_name' ,'', 'strip_tags');
            $params['area_1'] = I('post.mutil_level_data_1_id_1' , 0);
            $params['area_2'] = I('post.mutil_level_data_1_id_2' , 0);
            $params['area_3'] = I('post.mutil_level_data_1_id_3' , 0);
            $params['address'] = I('post.address' ,'', 'strip_tags');
            $params['contacter'] = I('post.contacter' ,'', 'strip_tags');
            $params['phone'] = I('post.phone' ,'', 'strip_tags');
			$params['is_online'] = I('post.is_online', 1);
            $params['recharge_method_id'] = I('post.recharge_method_id', 0);
            $params['agent_id'] = I('post.agent_id', 0);
			$params['agent_level_1'] = I('post.agent_level_1', 0);
			$params['agent_level_2'] = I('post.agent_level_2', 0);
			$params['type'] = I('post.type', 1);
			$params['is_try'] = I('post.is_try', 0);
			$params['try_day_max'] = I('post.try_day_max', 0);
            $params['video_limit'] = I('post.video_limit', 0);
			if($params['try_day_max']) {
				$params['try_day_max'] = strtotime($params['try_day_max'].' 23:59:59');
			}
            if(I('post.accredit_day_end')!=''){ //授权结束日期
                $params['accredit_day_end']=strtotime(I('post.accredit_day_end').' 23:59:59');
            }
			if(1 != $params['is_try']) {
				$params['try_day_max'] = 0;
			}

			$params['pay_position'] = I('post.pay_position', 0);
            $params['hall_price'] = I('post.hall_price', 0);
            $params['buy_hall_count'] = I('post.buy_hall_count', 0);
            $params['period_count'] = I('post.period_count', 0);
            $params['deduct_period'] = I('post.deduct_period', 0);
            $params['remark'] = I('post.remark', 0);
            $params['vpn_cinema_name'] = I('post.vpn_cinema_name','','trim,strip_tags');

			//2016计费方式
			$params['billing_video_play_type'] = I('post.billing_video_play_type');
			
			//片库类型
			$params['video_data_type'] = I('post.video_data_type');
			$params['video_net'] = I('post.video_net');
			
			//影院系统平台
			$params['system_platform'] = I('post.system_platform', 1);
			
			//是否支持本地视频
			$params['is_support_local_video'] = I('post.is_support_local_video', 2);
			
			//视频版权区域
			$params['video_copyright_area'] = I('post.video_copyright_area');
			//$params['video_copyright_area'] && $params['video_copyright_area'] = implode(',', $params['video_copyright_area']);
			//!$params['video_copyright_area'] && $params['video_copyright_area'] = '';
			
			//视频版权行业
			$params['video_copyright_industry'] = I('post.video_copyright_industry');
			//$params['video_copyright_industry'] && $params['video_copyright_industry'] = implode(',', $params['video_copyright_industry']);
			//!$params['video_copyright_industry'] && $params['video_copyright_industry'] = '';
			
			//影院签订的版权商
			$params['copyright_user_ids'] = I('post.copyright_user_ids');
			$params['copyright_user_ids'] && $params['copyright_user_ids'] = implode(',', $params['copyright_user_ids']);
			!$params['copyright_user_ids'] && $params['copyright_user_ids'] = '';
			
			$params['bank_card_account'] = I('post.bank_card_account' ,'', 'strip_tags');
			$params['bank_card_type'] = I('post.bank_card_type');
			$params['bank_card_bank'] = I('post.bank_card_bank');
			$params['bank_card_number'] = I('post.bank_card_number' ,'', 'strip_tags');
			$params['bank_card_address'] = I('post.bank_card_address' ,'', 'strip_tags');
			
			$params['auth_pic'] = I('post.auth_pic' ,'', 'strip_tags');
			$params['auth_code_1'] = I('post.auth_code_1' ,'', 'strip_tags');
			$params['auth_code_2'] = I('post.auth_code_2' ,'', 'strip_tags');

            $params['id'] = $id;
            $params['user_id'] = $user_info['user_id'];


            $result = $cinemaLogic->edit($params);

             //add by Chuck 2015-11-10 影院上下线时更改用户收藏影院的状态
            if($params['is_online'] == 1) {
                M('customer_fav')->where(array('cinema_id' => $id))->save(array('status' => 1));
            } else if($params['is_online'] == 2) {
                M('customer_fav')->where(array('cinema_id' => $id))->save(array('status' => 0));//在用户收藏列表中不显示
            }

            if(1 == $result['status_code']) {
				//数据处理成功，如果是影院类型为小帅，更新数据到小帅表
				if(4 == $params['type']) {
					$xs_hotel_params = array();
					$xs_hotel_params['host_name'] = $model['host_name'];
					$xs_hotel_params['qudao_type'] = 0;
					$cinemaLogic->updateXsHotel($xs_hotel_params);
				}elseif(8 == $params['type']) {
					$xs_hotel_params = array();
					$xs_hotel_params['host_name'] = $model['host_name'];
					$xs_hotel_params['qudao_type'] = 1;
					$cinemaLogic->updateXsHotel($xs_hotel_params);
				}elseif(9 == $params['type']) {
					$xs_hotel_params = array();
					$xs_hotel_params['host_name'] = $model['host_name'];
					$xs_hotel_params['qudao_type'] = 2;
					$cinemaLogic->updateXsHotel($xs_hotel_params);
				}elseif(10 == $params['type']) {
					$xs_hotel_params = array();
					$xs_hotel_params['host_name'] = $model['host_name'];
					$xs_hotel_params['qudao_type'] = 3;
					$cinemaLogic->updateXsHotel($xs_hotel_params);
				}elseif(11 == $params['type']) {
					$xs_hotel_params = array();
					$xs_hotel_params['host_name'] = $model['host_name'];
					$xs_hotel_params['qudao_type'] = 4;
					$cinemaLogic->updateXsHotel($xs_hotel_params);
				}elseif(3 == $params['type']){
                    //D('Common/KmiCron','Logic')->createKmiCinema($id);
                }

                if($params['type']!=2){
                    D('Common/KmiCron','Logic')->createKmiCinema($id,$params['type']); //创建播放日志表
                    if($model['type']!=$params['type']){
                        $change_arr = ['id'=>$model['id'],'host_name'=>$model['host_name'],'old_type'=>$model['type'],'type'=>$params['type']];
                        D('Common/Cache','Logic')->RPUSH('ChangeCinemaType',json_encode($change_arr));
                    }
                }

                $referer = htmlspecialchars_decode(I('post.referer', U('Cinema/acountList')));
                $this->success($result['status_message'], $referer, 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }

        //获取用户组选项
        $user_group_options = $userLogic->getUserGroupOptions();
        
        //获取影院计费方式
        $recharge_method_params = array();
        $recharge_method_params['status'] = 1;
		$recharge_method_params['empty'] = 1;
		$recharge_method_params['recharge_method_ids'] = '13,14';
        $recharge_method_options = $cinemaLogic->getRechargeMethodOptions($recharge_method_params);

        //代理选项
        $agent_options = D('Admin/agent')->getAgentOption();
        $agent_options = array(''=>'平台直属影院')+$agent_options;
        
        //扣费周期
        $deduct_period_options = getDeductPeriodOptions();
        
		//影院类型
        $cinema_type_options = getCinemaTypeOptions();
		
		//扣费先后
        $pay_position_options = getPayPositionOptions();
		
		//是否选项
		$yes_no_options = getYesOrNo();
		
		//银行选项
        $bank_options = getBankOptions(1);
		
		//银行卡类型选项
        $bank_card_type_options = getBankCardTypeOptions(1);
		
		//片源类型
        $video_data_type_options = getVideoDataTypeOptions(1);
		
		//片源网络
        $video_net_options = getVideoNetOptions();
		
		//2016计费方式
		$billing_video_play_type = C('CINEMA_BILLING_VIDEO_PLAY_TYPE');
		$billing_video_play_type = array(0=>'请选择') + $billing_video_play_type;
		
		//获取影院系统平台
        $system_platform_options = getSystemPlatformOptions();
		
		//影院影片版权区域
        $video_copyright_area_options = getVideoCopyrightAreaOptions(1);
		$checkbox_params = array();
        $checkbox_params['options'] = $video_copyright_area_options;
        $checkbox_params['name'] = 'video_copyright_area';
        $checkbox_params['separator'] = '&nbsp;&nbsp;&nbsp;&nbsp;';
        $checkbox_params['class'] = 'video_copyright_area';
        $checkbox_params['checked'] = $model['video_copyright_area_arr'];
        $video_copyright_area_options_form = formCheckbox($checkbox_params);
		$video_copyright_area_options = array(''=>'请选择') + $video_copyright_area_options;
		
		//影院影片版权行业
        $video_copyright_industry_options = getVideoCopyrightIndustryOptions(1);
		$checkbox_params = array();
        $checkbox_params['options'] = $video_copyright_industry_options;
        $checkbox_params['name'] = 'video_copyright_industry';
        $checkbox_params['separator'] = '&nbsp;&nbsp;&nbsp;&nbsp;';
        $checkbox_params['class'] = 'video_copyright_industry';
        $checkbox_params['checked'] = $model['video_copyright_industry_arr'];
        $video_copyright_industry_options_form = formCheckbox($checkbox_params);
		$video_copyright_industry_options = array(''=>'请选择') + $video_copyright_industry_options;

		//版权商选项
		$configModel = M('Config');
		$video_film_source_options = $configModel->where('k="video_film_source"')->find();
		$video_film_source_options = json_decode($video_film_source_options['v'], true);
		$checkbox_params = array();
        $checkbox_params['options'] = $video_film_source_options;
        $checkbox_params['name'] = 'copyright_user_ids';
        $checkbox_params['separator'] = '&nbsp;&nbsp;&nbsp;&nbsp;';
        $checkbox_params['class'] = 'copyright_user_ids';
        $checkbox_params['checked'] = $model['copyright_user_ids_arr'];
        $video_film_source_options_form = formCheckbox($checkbox_params);
		
        $is_add = 0;
        $page_title = '编辑用户';
        $this->assign(get_defined_vars());	
        $this->display('cinema');
    }
    
    //影院详情
    public function detail(){
        
        $userLogic = new \Common\Logic\UserLogic();
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        $logic= new \Common\Logic\BillingVideoPlayLogic();
        
        $id = I('request.id');
        
        //获取影院计费方式
        $recharge_method_params = array();
		$recharge_method_params['empty'] = 1;
        $recharge_method_params['status'] = 1;
        $recharge_method_options = $cinemaLogic->getRechargeMethodOptions($recharge_method_params);
        
        //查询当前影院用户
        $model = $cinemaLogic->find($id);
        $model['cinema_name']=$logic->selectCinemaName($id);

        //代理选项
        $agent_options = D('Admin/agent')->getAgentOption();
        $agent_options = array(''=>'平台直属影院')+$agent_options;
        
        //扣费周期
        $deduct_period_options = getDeductPeriodOptions();
        
		//2016计费方式
		$billing_video_play_type = C('CINEMA_BILLING_VIDEO_PLAY_TYPE');
		$billing_video_play_type = array(0=>'--') + $billing_video_play_type;
		
        $is_add = 0;
        $page_title = '详情';
        $this->assign(get_defined_vars());
        $this->display('detail');
    }
    
    //更新影院用户
    public function update(){
        
        $userLogic = new \Common\Logic\UserLogic();
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        //当前用户信息
        $user_info = $userLogic->getCurrentUserInfo();
        
        $params = array();
        $params['id'] = I('request.id');
        $status = I('request.power_status');
        if($status) {
            $params['power_status'] = $status;
        }
        $params['user_id'] = $user_info['user_id'];
        $result = $cinemaLogic->update($params);
        if(1 == $result['status_code']) {
            $this->success($result['status_message'], C('REFERER'), 3);
        } else {
            $this->error($result['status_message'], C('REFERER'), 3);
        }
    }
    
    //计费方式列表
    public function rechargeMethodList(){
        
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        $params = array();
        $params['page_size'] = 15;
        $data_list_data = $cinemaLogic->getRechargeMethodList($params);
        
        $this->assign(get_defined_vars());
        $this->display('rechargeMethodList');
    }
    
    //添加计费方式
    public function addRechargeMethod(){
        
        $userLogic = new \Common\Logic\UserLogic();
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        if(IS_POST) {
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            
            $params = array();
            $params['recharge_method_name'] = I('post.recharge_method_name' ,'', 'strip_tags');
            $params['description'] = I('post.description' ,'', 'strip_tags');
            $params['cost_type'] = I('post.cost_type', 1, 'strip_tags');
            $params['time_count'] = I('post.time_count', 1);
            $params['time_type'] = I('post.time_type', 1);
            $params['price'] = I('post.price', 0);
            $params['user_id'] = $user_info['user_id'];
            $result = $cinemaLogic->addRechargeMethod($params);
            if(1 == $result['status_code']) {
                $this->success($result['status_message'], U('Cinema/rechargeMethodList'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
        
        $page_title = '添加计费方式';
        $this->assign(get_defined_vars());
        $this->display('rechargeMethod');
    }
    
    //更新计费方式
    public function updateRechargeMethod(){
        
        $userLogic = new \Common\Logic\UserLogic();
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        //当前用户信息
        $user_info = $userLogic->getCurrentUserInfo();
        
        $params = array();
        $params['recharge_method_id'] = I('request.recharge_method_id');
        $status = I('request.status');
        if($status) {
            $params['status'] = $status;
        }
        $params['user_id'] = $user_info['user_id'];
        $result = $cinemaLogic->updateRechargeMethod($params);
        if(1 == $result['status_code']) {
            $this->success($result['status_message'], C('REFERER'), 3);
        } else {
            $this->error($result['status_message'], C('REFERER'), 3);
        }
    }
    
    //充值列表
    public function rechargeList(){
        
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        //查询影院用户的充值记录
        $params = array();
        $params['page_size'] = 20;
        $params['recharge_type'] = 1;
		$keywords = I('request.keywords');
		if($keywords === 0){
			 $params['keywords'] =  array('eq',0);;
		}else{
			$params['keywords'] = $keywords;
		}
        $params['create_time_start'] = $create_time_start = I('get.create_time_start');
        $params['create_time_end'] = $create_time_end = I('get.create_time_end');
		$params['status'] = 1;
		$params['recharge_from'] = I('request.recharge_from');
        $recharge_list_data = $cinemaLogic->getRechargeList($params);
        
        //获取影院计费方式
        $recharge_method_options = $cinemaLogic->getRechargeMethodOptions();
        
        $page_title = '充值流水';
        $this->assign(get_defined_vars());
        $this->display('rechargeList');
    }
    
    //充值
    public function recharge()
    {
        $userLogic = new \Common\Logic\UserLogic();
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        $id = I('request.id');
        
        if(IS_POST) {
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            
            $params = array();
            $params['money'] = I('post.money' , 0);
            $params['description'] = I('post.description' ,'', 'strip_tags');
            $params['img_url'] = I('post.img_url', '', 'strip_tags');
            $params['account_name'] = I('post.account_name', '', 'strip_tags');
            $params['account_number'] = I('post.account_number', '', 'strip_tags');
            $params['cinema_host_id'] = $id;
            $params['recharge_type'] = 1;
            $params['recharge_from'] = I('post.recharge_from', 1);
            
            //是否交易完成；(0；否，预充值)
            $params['is_complete'] = 0;
            $params['user_id'] = $user_info['user_id'];
            $result = $cinemaLogic->recharge($params);
            if(1 == $result['status_code']) {
                $referer = htmlspecialchars_decode(I('post.referer', U('Cinema/acountList')));
                $this->success($result['status_message'], $referer, 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
        
        //查询当前影院用户
        $cinema_host = $cinemaLogic->find($id);
        
        //获取影院计费方式
        $recharge_type_options = getRechargeTypeOptions();
        
        //获取公司充值银行
        $bank_account_options = getBankAccountOptions();
        
        //获取公司充值银行：账号对照表
        $bank_account_map_options = getBankAccountMapOptions();
        
        $page_title = '充值申请';
        $this->assign(get_defined_vars());
        $this->display('recharge');
    }
    
    //充值详情
    public function rechargeDetail(){
        
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        $id = I('request.id');
        $recharge_log_id = I('request.recharge_log_id');
        
        //查询当前充值详情
        $model = $cinemaLogic->findRechargeDetail($recharge_log_id);
        
        //查询当前影院用户
        $cinema_host = $cinemaLogic->find($id);
        
        //获取影院计费方式
        $recharge_method_options = $cinemaLogic->getRechargeMethodOptions();
        
        //获取公司充值银行
        $bank_account_options = getBankAccountOptions();
        
        $page_title = '详情';
        $this->assign(get_defined_vars());
        $this->display('rechargeDetail');
    }
    
    //影院用户计费详情：充值记录
    public function userRechargeList(){
        
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        $id = I('request.id');
        
        //查询当前影院用户的充值记录
        $params = array();
        $params['page_size'] = 20;
        $params['keywords'] = $keywords = I('request.keywords', '', 'strip_tags');
        $params['cinema_host_id'] = $id;
        $params['recharge_type'] = 1;
        $params['create_time_start'] = $create_time_start = I('get.create_time_start');
        $params['create_time_end'] = $create_time_end = I('get.create_time_end');
        $data_list_data = $cinemaLogic->getRechargeList($params);
        
        $page_title = '计费详情>>充值流水';
        $this->assign(get_defined_vars());
        $this->display('userRechargeList');
    }
    
    //影院用户计费详情：消费记录
    public function userCostList(){
        
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        $id = I('request.id');
        
        //查询当前影院用户的充值记录
        $params = array();
        $params['page_size'] = 20;
        $params['keywords'] = $keywords = I('request.keywords', '', 'strip_tags');
        $params['cinema_host_id'] = $id;
        $params['recharge_type'] = 2;
        $params['create_time_start'] = $create_time_start = I('get.create_time_start');
        $params['create_time_end'] = $create_time_end = I('get.create_time_end');
        $data_list_data = $cinemaLogic->getRechargeList($params);
        
        //获取影院计费方式
        $recharge_method_options = $cinemaLogic->getRechargeMethodOptions();
        
        $page_title = '计费详情>>消费流水';
        $this->assign(get_defined_vars());
        $this->display('userCostList');
    }
    
    //密码初始化
    public function initPassword(){
        
        $userLogic = new \Common\Logic\UserLogic();
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        if(IS_POST) {
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            $params = array();
            $params['init_password'] = md5('123456');
            $params['id'] = I('request.id', 0);
            $params['user_id'] = $user_info['user_id'];
            
            $result = $cinemaLogic->update($params);
            if(1 == $result['status_code']) {
                $this->success($result['status_message'], C('REFERER'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
    }
    
    //计费变更
    public function changeRecharge()
    {
        $userLogic = new \Common\Logic\UserLogic();
        $cinemaLogic = new \Common\Logic\CinemaLogic();
        
        $id = I('request.id');
        
        if(IS_POST) {
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            
            $params = array();
            $params['hall_price_next'] = I('post.hall_price_next', 0);
            $params['buy_hall_count_next'] = I('post.buy_hall_count_next', 0);
            $params['period_count_next'] = I('post.period_count_next', 0);
            $params['deduct_period_next'] = I('post.deduct_period_next', 0);
            $params['remark_next'] = I('post.remark_next', 0);
            $params['id'] = $id;
            $params['user_id'] = $user_info['user_id'];
            $result = $cinemaLogic->changeRecharge($params);
            if(1 == $result['status_code']) {
                $this->success($result['status_message'], U('Cinema/acountList'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
        
        //查询当前影院用户
        $model = $cinemaLogic->find($id);
        
        //扣费周期
        $deduct_period_options = getDeductPeriodOptions();
        
        $page_title = '计费变更';
        $this->assign(get_defined_vars());
        $this->display('changeRecharge');
    }

	//查询影院授权凭证：niem
	public function checkAuthInfo()
	{
		$commonLogic = new \Common\Logic\CommonLogic();

		$params = array();
		$params['auth_code_1'] = I('request.auth_code_1', '', 'strip_tags');
		$params['auth_code_2'] = I('request.auth_code_2', '', 'strip_tags');
		$params['id'] = I('get.id', '', 'strip_tags');
		$result = $commonLogic->checkAuthInfo($params);
		$this->ajaxReturn($result);
	}
	
	//设置影院类型版权信息
	public function setCinemaTypeCopyrightInfo()
	{
		$configModel = M('Config');
		
		//影院类型
        $cinema_type_options = getCinemaTypeOptions();
		
		if(IS_POST) {
			$cinema_type_copyright_data = json_encode($_POST);
			$update_data = array();
			$update_data['v'] = $cinema_type_copyright_data;
			$video_film_source_options = $configModel->where('k="cinema_type_copyright_data"')->data($update_data)->save();
			$result['status_message'] = '设置成功';
			$this->success($result['status_message'], C('REFERER'), 3);
        }
		
		//查询当前配置数据
		$cinema_type_copyright_data = $configModel->where('k="cinema_type_copyright_data"')->find();
		$cinema_type_copyright_data = json_decode($cinema_type_copyright_data['v'], true);
		
		//影院影片版权区域
        $video_copyright_area_options = getVideoCopyrightAreaOptions(1);
		$checkbox_params = array();
        $checkbox_params['options'] = $video_copyright_area_options;
        $checkbox_params['name'] = 'video_copyright_area';
        $checkbox_params['separator'] = '&nbsp;&nbsp;&nbsp;&nbsp;';
        $checkbox_params['class'] = 'video_copyright_area';
        $checkbox_params['checked'] = $model['video_copyright_area_arr'];
        $video_copyright_area_options_form = formCheckbox($checkbox_params);
		$video_copyright_area_options = array(''=>'请选择') + $video_copyright_area_options;
		
		//影院影片版权行业
        $video_copyright_industry_options = getVideoCopyrightIndustryOptions(1);
		$checkbox_params = array();
        $checkbox_params['options'] = $video_copyright_industry_options;
        $checkbox_params['name'] = 'video_copyright_industry';
        $checkbox_params['separator'] = '&nbsp;&nbsp;&nbsp;&nbsp;';
        $checkbox_params['class'] = 'video_copyright_industry';
        $checkbox_params['checked'] = $model['video_copyright_industry_arr'];
        $video_copyright_industry_options_form = formCheckbox($checkbox_params);
		$video_copyright_industry_options = array(''=>'请选择') + $video_copyright_industry_options;
		
		//版权商选项
		$video_film_source_options = $configModel->where('k="video_film_source"')->find();
		$video_film_source_options = json_decode($video_film_source_options['v'], true);
		$video_film_source_options_forms = array();
		foreach($cinema_type_options as $key => $value) {
			$checkbox_params = array();
			$checkbox_params['options'] = $video_film_source_options;
			$checkbox_params['name'] = 'copyright_user_ids_'.$key;
			$checkbox_params['separator'] = '&nbsp;&nbsp;&nbsp;&nbsp;';
			$checkbox_params['class'] = 'copyright_user_ids';
			$checkbox_params['checked'] = $cinema_type_copyright_data['copyright_user_ids_'.$key];
			$video_film_source_options_forms[$key] = formCheckbox($checkbox_params);
		}
		
		$page_title = '设置影院类型版权信息';
        $this->assign(get_defined_vars());
        $this->display('setCinemaTypeCopyrightInfo');
	}
	public function ylOrderList(){

        if(IS_GET){
            $params['page']=I('p',1,'strip_tags');
            $params['page_size'] = 10;
            $params['start']=I('start','','strip_tags');
            $params['end']=I('end','','strip_tags');
            $params['pay_type']=I('pay_type','','strip_tags');
            $params['host_name']=I('host_name','','strip_tags');
            $where['_string']='1=1';
            if($params['start'] && $params['end']){
                $where['order_time']=array('between',array($params['start'],$params['end']));
            }else if($params['start']){
                $where['order_time']=array('egt',$params['start']);
            }else if($params['end']){
                $where['order_time']=array('elt',$params['end']);
            }
            if($params['pay_type']){
                $where['pay_type']=$params['pay_type'];
            }
            if($params['host_name']!==''){
                $where['host_name']=$params['host_name'];
            }
            $Yl=M('yl_order');
            $data=array();
            $count=$Yl->where($where)->count('id');
            $pager = new \Think\Page2($count, $params['page_size']);
            $data=$Yl->page($params['page'],$params['page_size'])->where($where)->order('order_time')->select();
            $result['data_list']=$data;
            $result['pager']=$pager->show();
            $this->assign('datas',$result);

            //获取医院信息
            $cinemas=M('yl_order')->alias('a')->join('left join mt_cinema_host as b on a.host_name=b.host_name')->group('a.host_name')->getField('b.id,b.host_name,b.vpn_cinema_name',true);
            $hosts=array();
            foreach ($cinemas as $vo){
               $hosts[$vo['host_name']]=$vo['vpn_cinema_name'];
           }
           $this->count0=$Yl->count('id');
           $this->count_money0=$Yl->sum('money');
           $this->count=$Yl->where($where)->count('id');
           $this->count_money=$Yl->where($where)->sum('money');
           $this->assign('hosts',$hosts);
        }
        $this->display();
    }
}
